"""
嵌套列表法代码
"""


def BinaryTree(r):
    return [r, [], []]


def insertLeft(r, newBranch):
    t = r.pop(1)
    if len(t) > 1:
        r.insert(1, [newBranch, t, []])
    else:
        r.insert(1, [newBranch, [], []])
    return r


def insertRight(r, newBranch):
    t = r.pop(2)
    if len(t) > 1:
        r.insert(2, [newBranch, [], t])
    else:
        r.insert(2, [newBranch, [], []])
    return r


def getRootVal(r):
    return r[0]


def setRootVal(r, newval):
    r[0] = newval


def getLeftChild(r):
    return r[1]


def getRightChild(r):
    return r[2]


r = BinaryTree(3)
insertLeft(r, 4)
insertLeft(r, 5)
insertRight(r, 6)
insertRight(r, 7)
i = getLeftChild(r)
print(i)

setRootVal(i, 9)
print(r)
insertLeft(i, 11)
print(r)
print(getRightChild((getRightChild(r))))
